package com.xbongbong.pro.dataset.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.XbbConstant;
import com.xbongbong.paas.domain.entity.PaasFormSubDataEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.dataset.service.DataSetSubFormService;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.statistic.help.ChartResultHelp;
import com.xbongbong.pro.statistic.pojo.ChartFieldAttrPojo;
import com.xbongbong.pro.statistic.pojo.dto.DataSetSubFormListDTO;
import com.xbongbong.pro.statistic.pojo.dto.DataSetSubFormListItemsDTO;
import com.xbongbong.pro.statistic.pojo.vo.ChartSubFormListVO;
import com.xbongbong.saas.domain.entity.ext.ProductEntityExt;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.model.ProductModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 描述信息
 *
 * @ClassName DataSetSubFormServiceImpl
 * @Author 梁鲁江
 * 创建时间 2022/10/20 11:12
 * 修改时间  2022/10/20 11:12
 **/
@Service("dataSetSubFormService")
public class DataSetSubFormServiceImpl implements DataSetSubFormService {
    @Resource
    private ChartResultHelp chartResultHelp;
    @Resource
    private ProductModel productModel;
    @Override
    public ChartSubFormListVO chartGetSubFormList(DataSetSubFormListDTO dataSetSubFormListDTO) throws XbbException {
        ChartSubFormListVO chartSubFormListVO = new ChartSubFormListVO();
        List<DataSetSubFormListItemsDTO>  items = dataSetSubFormListDTO.getItems();
        String valueAttr = dataSetSubFormListDTO.getValue();
        if(CollectionUtils.isEmpty(items) || StringUtil.isEmpty(valueAttr)){
            return chartSubFormListVO;
        }
        //表头字段组装
        List<ChartFieldAttrPojo> headList = new ArrayList<>(items.size());
        for (DataSetSubFormListItemsDTO item : items) {
            ChartFieldAttrPojo chartFieldAttrPojo = new ChartFieldAttrPojo();
            BeanUtil.copyProperties(item.getFieldAttrPojo(),chartFieldAttrPojo);
            headList.add((chartFieldAttrPojo) );
        }
        List<String> includes = new ArrayList<>();
        Map<String, ChartFieldAttrPojo> headAttrMap = new HashMap<>(1 << 4);
        headList.forEach(e -> {
            includes.add(e.getAttr());
            headAttrMap.put(e.getAttr(), e);
        });
        includes.add(FieldTypeEnum.ID.getAlias());
        chartSubFormListVO.setHeadList(headList);
        String[] valueArray = valueAttr.split(XbbConstant.THE_CROSSED);
        Long dataId = Long.parseLong(valueArray[0]);
        Long formId = Long.parseLong(valueArray[1]);
        Integer businessType = Integer.parseInt(valueArray[2]);
        Integer saasMark = Integer.parseInt(valueArray[3]);
        String subFieldAttr = valueArray[4];
        String corpid = dataSetSubFormListDTO.getCorpid();
        IndexTypeEnum indexTypeEnum = chartResultHelp.getSubFormIndexTypeEnum(saasMark, businessType, formId, corpid);
        PageHelper pageHelper = new PageHelper(1);
        if (dataSetSubFormListDTO.getPage() != null && dataSetSubFormListDTO.getPageSize() != null) {
            pageHelper = new PageHelper(dataSetSubFormListDTO.getPage(), dataSetSubFormListDTO.getPageSize());
        }
        chartSubFormListVO.setPageHelper(pageHelper);
        dataId = productModel.getParentDataId(dataId, businessType, corpid);
        //获取子表单数据
        List<? extends PaasFormSubDataEntity> entityList = chartResultHelp.getPaasFormSubDataByIdAndAttr(corpid, dataId, indexTypeEnum, subFieldAttr, pageHelper);
        //获取子表单数据的总条数
        long count = chartResultHelp.getPaasFormSubDataCount(dataSetSubFormListDTO, dataId, indexTypeEnum, subFieldAttr, pageHelper);
        //attr字段对应结果集映射关系
        Map<String, List<String>> attrToValuesMap = new HashMap<>(1 << 4);
        entityList.forEach(e -> {
            JSONObject dataObj = e.getData();
            dataObj.forEach((attr, value) -> {
                List<String> obj = new ArrayList<>();
                if (value == null) {
                    value = "";
                }
                obj.add(value.toString());
                attrToValuesMap.merge(attr, obj, (v1, v2) -> {
                    v1.addAll(v2);
                    return v1;
                });
            });
        });
        //字段解析
        ChartEntity chartEntity = new ChartEntity();
        chartEntity.setCorpid(corpid);
        chartResultHelp.analysisSubFormData(headAttrMap, attrToValuesMap, chartEntity, entityList, dataSetSubFormListDTO);
        chartSubFormListVO.setResults(entityList);
        chartSubFormListVO.setTotal(count);
        return chartSubFormListVO;
    }
    
}
